package com.yjf.ljr.activity;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.PreTransfersInvestDataClass;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppTransferPubDialog;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.view.DialogView;
import com.yjf.ljr.view.TransferPubDialog;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/***/
public class ConfirmationPurchaseActivity extends IjActivity implements OnClickListener {
	@ID("btConfirmationPurchase")
	private Button mBtConfirmationPurchase;
	private DialogView mDialogConfirmation;
	private View mViewDialogConfirmation;
	private Button mBtDialogRechargePurchaseOk;
	private Button mBtDialogRechargePurchaseCancel;
	public static ConfirmationPurchaseActivity instance;

	private String tradeTransferId;// 转让查询Id
	private String token;// 校验码
	private String yjfPaypassUrl;// 支付url
	private String needInvestAmout;// 需要投资的金额
	private String availableBalance;// 账户可用资金
	private String bizNo;// 业务码
	private String profitRate;// 年历化率
	private String amount;// 转让价格
	private String InterestAmount;// 预计收益
	private String tradeTransferIdPre;// 转让id
	private String tradeId;
	private String investAmount;
	private String tradeDetailId;// 关联投资id
	private TransferPubDialog.Builder mBuilder;
	@ID("tvConfirAvailableBalance")
	private TextView mTvConfirAvailableBalance;// 当前可用余额
	private static final int REQUESTDETORRECHARGE = 3000;

	@ID("tvConfirTransferPrice")
	private TextView mTvConfirTransferPrice;// 转让价格

	@ID("tvConfirAnnualInterestRate")
	private TextView mTvConfirAnnualInterestRate;// 年化利率

	@ID("tvConfirInterestAmount")
	private TextView mTvConfirInterestAmount;// 预计收益
	
	private String mobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_confirmation_purchase);
		initControl();
	}

	private void initControl() {
		setTitleStr("确认购买");
		setLeftBtnClick();
		instance = this;
		mBuilder = new TransferPubDialog.Builder(mContext);
		tradeId = getIntent().getStringExtra("tradeId");
		investAmount = getIntent().getStringExtra("investAmount");
		tradeDetailId = getIntent().getStringExtra("tradeDetailId");
		tradeTransferIdPre = getIntent().getStringExtra("tradeTransferId");
		profitRate = getIntent().getStringExtra("profitRate");
		amount = getIntent().getStringExtra("amount");
		InterestAmount = getIntent().getStringExtra("InterestAmount");
		
		mobile = getIntent().getStringExtra("mobile");

		mTvConfirTransferPrice.setText("￥" + AppUtil.changeMoneyCompany(amount));
		if (!TextUtils.isEmpty(profitRate) && profitRate.contains("%")) {
			mTvConfirAnnualInterestRate.setText(profitRate);
		} else {
			mTvConfirAnnualInterestRate.setText(profitRate + "%");

		}
		mTvConfirInterestAmount.setText("￥" + InterestAmount);

		mBtConfirmationPurchase.setOnClickListener(this);
		mViewDialogConfirmation = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirmation_purchase, null);
		mBtDialogRechargePurchaseOk = (Button) mViewDialogConfirmation.findViewById(R.id.btDialogRechargePurchaseOk);
		mBtDialogRechargePurchaseCancel = (Button) mViewDialogConfirmation
				.findViewById(R.id.btDialogRechargePurchaseCancel);
		mBtDialogRechargePurchaseOk.setOnClickListener(this);
		mBtDialogRechargePurchaseCancel.setOnClickListener(this);
		mDialogConfirmation = new DialogView(mContext, mViewDialogConfirmation);
		showProgressDialog();
		new PreTransfersInvestTask(tradeTransferIdPre).execute();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btConfirmationPurchase:
			ComparisonMoney();

			break;
		case R.id.btDialogRechargePurchaseOk:
			startActivityForResult(new Intent(ConfirmationPurchaseActivity.this, RechargeActivity.class),
					REQUESTDETORRECHARGE);
			mDialogConfirmation.dismiss();
			break;
		case R.id.btDialogRechargePurchaseCancel:
			mDialogConfirmation.dismiss();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case REQUESTDETORRECHARGE:
				showProgressDialog();
				new PreTransfersInvestTask(tradeTransferIdPre).execute();
				break;

			default:
				break;
			}

		}
	}

	/********* 债权转让金额判断 *****/
	private void ComparisonMoney() {
		if ((!TextUtils.isEmpty(mTvConfirAvailableBalance.getText().toString())) && (!(TextUtils.isEmpty(amount)))) {
			double totalMoney = Double.parseDouble(mTvConfirAvailableBalance.getText().toString().substring(1)
					.replace(",", ""));
			double transferPrice = Double.parseDouble(amount.replace(",", ""));
			if (totalMoney < transferPrice) {
				/** 余额不足弹出 ***/
				mDialogConfirmation.show();
			} else {
				mBuilder.setmobile(mobile);
				mBuilder.setToken(token);
				mBuilder.setbizNo(bizNo);
				mBuilder.settradeTransferId(tradeTransferIdPre);
				mBuilder.create().show();
			}
		}

	}

	/********** 获取购买债权前置信息 *************/
	private class PreTransfersInvestTask extends AsyncTask<Void, Void, String> {
		PreTransfersInvestDataClass dc = new PreTransfersInvestDataClass();
		RequestObject mObject = new RequestObject();
		private String tradeTransferId;

		public PreTransfersInvestTask(String tradeTransferId) {
			this.tradeTransferId = tradeTransferId;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			mObject.method = "preTransfersInvest";
			mObject.map.put("tradeTransferId", tradeTransferId);
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (TextUtils.isEmpty(result)) {
				tradeTransferId = dc.result.tradeTransferId;// 转让查询Id
				token = dc.result.token;// 校验码
				yjfPaypassUrl = dc.result.yjfPaypassUrl;// 支付url
				needInvestAmout = dc.result.needInvestAmout;// 需要投资的金额
				availableBalance = dc.result.availableBalance;// 账户可用资金
				bizNo = dc.result.bizNo;// 业务码
				mTvConfirAvailableBalance.setText("￥" + availableBalance);
			} else {
				showToast(result);
			}
			dismissProgressDialog();
		}
	}
}
